package com.wsdk.util;

import android.text.TextUtils;

import com.wsdk.bean.VersionStateData;

public class ParseBluetoothData {

    public static VersionStateData parseVersion(byte[] data) {
        if (data[3] == 0x02) {
            //02 1100 02 00 00010200     00       14     00       64      00   00    01  01  00   60 03
            //               版本号   初始化状态  电量  充电状态 电池指标  复位 锁关 锁开 盖章 锁状
            if (data[4] == 0x00) {
                VersionStateData versionStateData = new VersionStateData();
                byte[] version = {data[5], data[6], data[7], data[8]};
                versionStateData.setFirmwareVersion(bytesToHexString(version));
                versionStateData.setInitialStatus(bytesToHexString(new byte[]{data[9]}));
                if (data[11] == 0x00) {
                    versionStateData.setCharging(false);
                }else{
                    versionStateData.setCharging(true);
                }
                if (data[13] == 0x00) {
                    versionStateData.setReset(false);
                } else {
                    versionStateData.setReset(true);
                }
                if (data[17] == 0x00) {
                    versionStateData.setLockStatus(true);
                } else {
                    versionStateData.setLockStatus(true);
                }
                return versionStateData;
            } else {
                ShowToast.shortTime("获取信印版本号发生错误");
                return null;
            }
        }
        return null;
    }

    public static String stampCode(byte[] data) {
        if (data[3] == 0x0c) {
            if (data[4] == 0x00) {
                byte[] code = new byte[4];
                System.arraycopy(data, 5, code, 0, 4);
                return bytesToHexString(code);
            } else {
                ShowToast.shortTime("换章过程出错");
                return null;
            }
        }
        return null;
    }

    public static String parseSN(byte[] data) {
        if (data[3] == 0x04) {
            if (data[4] == 0x00) {//执行没有问题。
                byte[] SN = new byte[15];
                System.arraycopy(data, 5, SN, 0, 15);
                return bytesToHexString(SN);
            } else {
                ShowToast.shortTime("获取序列号错误");
                return null;
            }
        }
        return null;
    }

    public static int parseInit(byte[] data) {
        int ret = -1;
        if (data[3] == 0x01) {
            switch (data[1]) {
                case 0x04:
                    // version 1
                    if (data[4] == 0x00 || data[4] == (byte) 0xFD) {
                        ret = 0;
                    } else {
                        ret = -1;
                    }
                    break;
                case 0x05:
                    // version 2
                    switch (data[5]) {
                        case 0x01:
                            if (data[4] == 0x00/* || data[4] == (byte) 0xFD*/) {
                                ret = 1;
                            } else {
                                ret = -2;
                            }
                            break;
                        case 0x02:
                            if (data[4] == 0x00/* || data[4] == (byte) 0xFD*/) {
                                ret = 2;
                            } else {
                                ret = -3;
                            }
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }
        }
        return ret;
    }

    public static boolean parseSyncTime(byte[] data) {
        if (data[3] == 0x0e) {
            if (data[4] == 0x00) {
                return true;
            } else {
                ShowToast.shortTime("同步时间发生错误");
                return false;
            }
        }
        return false;
    }

    public static byte parseStampReturn(byte[] data) {
        //02 0700 0b 00 0000 00 4e03
        String hex = bytesToHexString(data);
        if(TextUtils.isEmpty(hex)){
            return (byte) 0xff;
        }
        if(hex.startsWith("0207000b00")){
            return data[7];
        }else{
            return (byte) 0xff;
        }
    }

    public static boolean parseOpen(byte[] data) {
        if (data[3] == 0x07) {
            if (data[4] == 0x00) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    public static int parseOpenByte(byte[] data) {
        if (data[3] == 0x07) {
            if (data[4] == 0x00) {
                return 0x00;
            } else if ((data[4] + 256) == 0xec) {
                return 0xec;
            } else if ((data[4] + 256) == 0xeb) {
                return 0xeb;
            } else {
                return 0xf4;
            }
        }
        return 0xff;
    }

    public static boolean parseDeleteAlert(byte[] data) {
        if (data[3] == 0x0f) {
            if (data[4] == 0x00) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    public static boolean parseLock(byte[] data) {
        if (data[3] == 0x08) {
            if (data[4] == 0x00) {
                return true;
            } else {
                ShowToast.shortTime("上锁错误");
                return false;
            }
        }
        return false;
    }

    public static boolean parseClearAll(byte[] data){
        if(data[3] == 0x11){
            if(data[4] == 0x00){
                return true;
            }else{
                ShowToast.shortTime("总清失败");
                return false;
            }
        }
        return false;
    }

    public static boolean isClearAll(byte[] data){
        return data[3] == 0x11;
    }

    public static boolean isSycTime(byte[] data) {
        return data[3] == 0x0e;
    }

    public static boolean isInit(byte[] data) {
        return data[3] == 0x01;
    }

    public static boolean isCheckVersion(byte[] data) {
        return data[3] == 0x02;
    }

    public static boolean isOpen(byte[] data) {
        return data[3] == 0x07;
    }

    public static boolean isStampReturn(byte[] data) {
        return data[3] == 0x0b;
    }

    public static boolean isLock(byte[] data) {
        return data[3] == 0x08;
    }

    public static boolean isGetSN(byte[] data) {
        return data[3] == 0x04;
    }

    public static boolean isBossMode(byte[] data) {
        return data[3] == 0x0D;
    }

    public static boolean isOpenBoss(byte[] data) {
        if (data[3] == 0x0D) {
            if (data[5] == 0x00) {
                if (data[4] == 0x00) {
                    return true;
                } else {
                    ShowToast.shortTime("设置解锁模式失败");
                    return false;
                }
            } else {
                ShowToast.shortTime("指令异常[ee]");
                return false;
            }
        }
        return false;
    }

    public static boolean isWorkMode(byte[] data) {
        if (data[3] == 0x0D) {
            if (data[5] == 0x01) {
                if (data[4] == 0x00) {
                    return true;
                } else {
                    ShowToast.shortTime("设置工作模式失败");
                    return false;
                }
            } else {
                ShowToast.shortTime("指令异常[ee]");
                return false;
            }
        }
        return false;
    }

    public static boolean isRepairMode(byte[] data) {
        if (data[3] == 0x0D) {
            if (data[5] == 0x02) {
                if (data[4] == 0x00) {
                    return true;
                } else {
                    ShowToast.shortTime("设置维修模式失败");
                    return false;
                }
            } else {
                ShowToast.shortTime("指令异常[ee]");
                return false;
            }
        }
        return false;
    }

    public static boolean isCloseRepairMode(byte[] data) {
        if (data[3] == 0x0D) {
            if (data[5] == 0x03) {
                if (data[4] == 0x00) {
                    return true;
                } else {
                    ShowToast.shortTime("撤销维修模式失败");
                    return false;
                }
            } else {
                ShowToast.shortTime("指令异常[ee]");
                return false;
            }
        }
        return false;
    }

    public static int updateVersion(byte[] data) {
        if (data[3] == 0x1f) {
            if (data[4] == 0x00 && data[5] == 0x01) {
                return 0;
            } else if(data[5] == 0x01){
                return 1;
            } else {
                return 2;
            }
        }
        return 2;
    }

    public static boolean updateVersion2(byte[] data, int packageNo) {
        if (data[3] == 0x1f) {
            if (data[4] == 0x00 && data[5] == 0x02 ) {
                byte[] tmp = new byte[]{data[6], data[7], data[8], data[9]};
                byte[] length = int2bytes(packageNo);
                return TextUtils.equals(bytesToHexString(tmp), bytesToHexString(length));
            } else if(data[5] == 0x02) {
                ShowToast.shortTime("固件传输失败，重新升级");
                return false;
            } else {
                return false;
            }
        }
        return false;
    }

    public static boolean updateVersion2Fail(byte[] data){
        return data[3] == 0x1f && data[4] == (byte) 0xf3;
    }

    public static boolean updateVersion3Fail(byte[] data){
        return data[3] == 0x1f && data[4] == (byte) 0xf1;
    }

    public static boolean updateVersion3(byte[] data){
        if(data[3] == 0x1f){
            if (data[4] == 0x00 && data[5] == 0x03) {
                ShowToast.shortTime("固件升级完全成功");
                return true;
            } else if (data[5] == 0x03) {
                ShowToast.shortTime("固件升级失败");
            }
        }
        return false;
    }

    public static boolean isChangeStamp(byte[] data) {
        if (data[3] == 0x0C) {
            if (data[4] == 0x00) {
                return true;
            } else {
                ShowToast.shortTime("换章失败");
                return false;
            }
        }

        return false;
    }

    private static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (byte aSrc : src) {
            int v = aSrc & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public static String bytesToSn(byte[] src){
        char[] c=new char[15];
        for(int i=0;i<15;i++){
            c[i]=(char)((int)(src[i]));
        }
        return new String(c);
    }

    private static byte[] int2bytes(int num) {
        byte[] result = new byte[4];
        result[3] = (byte) ((num >>> 24) & 0xff);
        result[2] = (byte) ((num >>> 16) & 0xff);
        result[1] = (byte) ((num >>> 8) & 0xff);
        result[0] = (byte) ((num) & 0xff);
        return result;
    }
}
